package com.lantu.sys.controller;

import com.lantu.common.utils.JwtUtil;
import com.lantu.common.vo.Result;
import com.lantu.sys.entity.Students;
import com.lantu.sys.entity.User;
import com.lantu.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mabc
 * @since 2024-06-29
 */
@RestController
@RequestMapping("/user")
//@CrossOrigin// 这个注解将会准许这个控制器下所有配置都可以准许被跨域访问
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping("/all")
    public Result<List<User>> getAllusers() {
        List<User> list = userService.list();
        return Result.success(list,"查询成功");

    }
    @PostMapping("/login")
    public Result<Map<String,Object>> login(@RequestBody User user) {

       Map<String,Object> data = userService.login(user);
       if (data != null){
           return Result.success(data);
       }else{
           return Result.fail(20002,"用户名或密码错误");
       }

    }

    //获取用户信息接口
    @GetMapping("/info")
    public Result<Map<String,Object>> getUserInfo(@RequestParam("token") String token){
        //根据token获取用户信息，redis
        Map<String,Object> data = userService.getUserInfo(token);
        if(data != null){
            return Result.success(data);
        }
        return Result.fail(20003,"登录信息无效，请重新登录");
    }

    //注销接口
    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("X-Token") String token){
        userService.logout(token);
        return Result.success("退出登录");
    }

//    @PostMapping("/login")
//    public User login() {
//
//    }
}
